package com.lenovo.smarttraffic.ui.activity.stage2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.RechargeRecord;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.ToastUtils;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ChargeHistoryActivity extends BaseActivity {

    private ListView mListView;

    @Override
    protected int getLayout() {
        return R.layout.activity_charge_history;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
    }

    public void initView() {
        Toolbar mToolbar = findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(view -> finish());

        mListView = findViewById(R.id.listview);

    }


    public void initData() {
        //查询所有的记录
        List<RechargeRecord> allData = LitePal.order("date desc").find(RechargeRecord.class);
        List<String> allDataString = new ArrayList<>();
        if (allData.size() != 0) {
            for (RechargeRecord allDatum : allData) {
                String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(allDatum.getDate());
                String stringBuilder = "时间:" + format + "\t" +
                        "车牌:" + allDatum.getCarNumber() + "\t" +
                        "用户:" + allDatum.getOpPeople() + "\t" +
                        "金额:" + allDatum.getRechargeMoney() + "\t";
                allDataString.add(stringBuilder);
            }

            //将数据交给适配器显示
            ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this,
                    android.R.layout.simple_list_item_1, allDataString.toArray(new String[]{}));
            mListView.setAdapter(stringArrayAdapter);
        } else {
            ToastUtils.showToast(this, "没有充值记录", Toast.LENGTH_SHORT);
            finish();
        }
    }


}
